<?php
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 消失的彩虹海 <www.cccyun.cc>
// +----------------------------------------------------------------------
// | Date: 2016/6/1
// +----------------------------------------------------------------------


class QQSignIn
{
    private $uin;
    private $skey;
    private $p_skey;
    private $cookie;
    private $g_tk;
    private $g_tk1;
    private $g_tk2;
    private $msg = array();
    private $skeyzt = 0;

    function __construct($uin, $skey, $p_skey)
    {
        $this->uin = $uin;
        $this->skey = $skey;
        $this->p_skey = $p_skey;
        $this->cookie = "pt2gguin=o0{$uin}; uin=o0{$uin}; skey={$skey}; p_uin=o0{$uin}; p_skey={$skey};";
        $this->g_tk = $this->getGTK($skey);
        $this->g_tk1 = $this->getGTK($p_skey);
        $this->g_tk2 = $this->getGTK2($skey);
    }

    public function getMsg()
    {
        return $this->msg;
    }

    public function qd(){
        $this->fzqd();
        $this->vipqd();
        $this->lzqd();
        $this->hzqd();
    }

	public function pqd(){
		$url="http://iyouxi.vip.qq.com/ams3.0.php?g_tk=".$this->g_tk2."&pvsrc=102&ozid=511022&vipid=&actid=32961&format=json".time()."8777&cache=3654";
		$data = $this->get_curl($url,0,'http://youxi.vip.qq.com/m/wallet/activeday/index.html?_wv=3&pvsrc=102',$this->cookie);
		$arr = json_decode($data, true);
		if(array_key_exists('ret',$arr) && $arr['ret']==0){
			$this->msg[]='钱包签到成功！';
		}elseif($arr['ret']==37206){
			$this->msg[]='钱包签到失败！你没有绑定银行卡';
		}elseif($arr['ret']==10601){
			$this->msg[]='你今天已钱包签到！';
		}else{
			$this->msg[]='钱包签到失败！'.$arr['msg'];
		}	
	}

    public function fzqd()
    {
        $url = 'http://x.pet.qq.com/vip_platform?cmd=set_sign_info&format=json&_=' . time() . '9008';
        $data = $this->get_curl($url, 0, $url, $this->cookie);
        $arr = json_decode($data, true);
        if (array_key_exists('result', $arr) && $arr['result'] == 0) {
            $this->msg[] = $this->uin . ' 粉钻签到成功！';
        } elseif ($arr['result'] == -101) {
            $this->skeyzt = 1;
            $this->msg[] = $this->uin . ' 粉钻签到失败！SKEY已失效';
        } else {
            $this->msg[] = $this->uin . ' 粉钻签到失败！' . $arr['msg'];
        }
    }

    public function vipqd()
    {
        $url = 'http://vipfunc.qq.com/act/client_oz.php?action=client&g_tk=' . $this->g_tk2;
        $data = $this->get_curl($url, 0, $url, $this->cookie);
        if ($data == '{ret:0}')
            $this->msg[] = $this->uin . ' 会员面板签到成功！';
        else
            $this->msg[] = $this->uin . ' 会员面板签到失败！';

        //$url='http://vipfunc.qq.com/growtask/sign.php?cb=vipsign.signCb&action=daysign&actId=16&fotmat=json&t='.time().'141&g_tk='.$this->g_tk2;
        //$data=$this->get_curl($url,0,$url,$this->cookie);
        $data = $this->get_curl("http://iyouxi.vip.qq.com/ams3.0.php?_c=page&actid=23314&format=json&g_tk=" . $this->g_tk2 . "&cachetime=" . time(), 0, 'http://vip.qq.com/', $this->cookie);
        $arr = json_decode($data, true);
        if (array_key_exists('ret', $arr) && $arr['ret'] == 0)
            $this->msg[] = $this->uin . ' 会员网页版签到成功！';
        elseif ($arr['ret'] == 10601)
            $this->msg[] = $this->uin . ' 会员网页版今天已经签到！';
        elseif ($arr['ret'] == 10002) {
            $this->skeyzt = 1;
            $this->msg[] = $this->uin . ' 会员网页版签到失败！SKEY过期';
        } elseif ($arr['ret'] == 20101)
            $this->msg[] = $this->uin . ' 会员网页版签到失败！不是QQ会员！';
        else
            $this->msg[] = $this->uin . ' 会员网页版签到失败！' . $arr['msg'];

        $data = $this->get_curl('http://iyouxi.vip.qq.com/ams3.0.php?actid=52002&rand=0.27489888' . time() . '&g_tk=' . $this->g_tk2 . '&format=json', 0, 'http://vip.qq.com/', $this->cookie);
        //$data=$this->get_curl('http://iyouxi.vip.qq.com/ams3.0.php?actid=23868&rand=0.387115'.time().'&g_tk='.$this->g_tk2.'&sid='.$this->sid.'&format=json',0,'http://vip.qq.com/',$this->cookie);
        $arr = json_decode($data, true);
        if (array_key_exists('ret', $arr) && $arr['ret'] == 0)
            $this->msg[] = $this->uin . ' 会员手机端签到成功！';
        elseif ($arr['ret'] == 10601)
            $this->msg[] = $this->uin . ' 会员手机端今天已经签到！';
        elseif ($arr['ret'] == 10002) {
            $this->skeyzt = 1;
            $this->msg[] = $this->uin . ' 会员手机端签到失败！SKEY过期';
        } else
            $this->msg[] = $this->uin . ' 会员手机端签到失败！' . $arr['msg'];

        $data = $this->get_curl('http://iyouxi.vip.qq.com/ams3.0.php?_c=page&actid=54963&isLoadUserInfo=1&format=json&g_tk=' . $this->g_tk2, 0, 'http://vip.qq.com/', $this->cookie);
        $arr = json_decode($data, true);
        if (array_key_exists('ret', $arr) && $arr['ret'] == 0)
            $this->msg[] = $this->uin . ' 会员积分签到成功！';
        elseif ($arr['ret'] == 10601)
            $this->msg[] = $this->uin . ' 会员积分今天已经签到！';
        elseif ($arr['ret'] == 10002) {
            $this->skeyzt = 1;
            $this->msg[] = $this->uin . ' 会员积分签到失败！SKEY过期';
        } else
            $this->msg[] = $this->uin . ' 会员积分签到失败！' . $arr['msg'];

        $data = $this->get_curl('http://iyouxi.vip.qq.com/ams2.02.php?actid=23074&g_tk_type=1sid=&rand=0.8656469448520889&format=json&g_tk=' . $this->g_tk2, 0, 'http://vip.qq.com/', $this->cookie);
        $arr = json_decode($data, true);
        if (array_key_exists('ret', $arr) && $arr['ret'] == 0)
            $this->msg[] = $this->uin . ' 会员积分2签到成功！';
        elseif ($arr['ret'] == 10601)
            $this->msg[] = $this->uin . ' 会员积分2今天已经签到！';
        elseif ($arr['ret'] == 10002) {
            $this->skeyzt = 1;
            $this->msg[] = $this->uin . ' 会员积分2签到失败！SKEY过期';
        } else
            $this->msg[] = $this->uin . ' 会员积分2签到失败！' . $arr['msg'];

        $this->get_curl('http://iyouxi.vip.qq.com/ams3.0.php?_c=page&actid=27754&g_tk=' . $this->g_tk2 . '&pvsrc=undefined&ozid=509656&vipid=MA20131223091753081&format=json&_=' . time(), 0, 'http://vip.qq.com/', $this->cookie);//超级会员每月成长值
        $this->get_curl('http://iyouxi.vip.qq.com/ams3.0.php?_c=page&actid=27755&g_tk=' . $this->g_tk2 . '&pvsrc=undefined&ozid=509656&vipid=MA20131223091753081&format=json&_=' . time(), 0, 'http://vip.qq.com/', $this->cookie);//超级会员每月积分
        $this->get_curl('http://iyouxi.vip.qq.com/ams3.0.php?g_tk=' . $this->g_tk2 . '&actid=22249&_c=page&format=json&_=' . time(), 0, 'http://vip.qq.com/', $this->cookie);//每周薪水积分
        $this->get_curl("http://iyouxi.vip.qq.com/jsonp.php?_c=page&actid=5474&isLoadUserInfo=1&format=json&g_tk=" . $this->g_tk2 . "&_=" . time(), 0, 0, $this->cookie);
    }

    public function lzqd()
    {
        $url = 'http://share.music.qq.com/fcgi-bin/dmrp_activity/fcg_feedback_send_lottery.fcg?activeid=110&rnd=' . time() . '157&g_tk=' . $this->g_tk . '&uin=' . $this->uin . '&hostUin=0&format=json&inCharset=UTF-8&outCharset=UTF-8&notice=0&platform=activity&needNewCode=1';
        $data = $this->get_curl($url, 0, 'http://y.qq.com/vip/fuliwo/index.html', $this->cookie);
        $arr = json_decode($data, true);
        if (array_key_exists('code', $arr) && $arr['code'] == 0) {
            if ($arr['data']['alreadysend'] == 1)
                $this->msg[] = '您今天已经签到过了！';
            else
                $this->msg[] = '绿钻签到成功！';
        } elseif ($arr['code'] == -200017) {
            $this->msg[] = '你不是绿钻无法签到！';
        } else {
            $this->msg[] = '绿钻签到失败！';
        }

        $url = 'http://share.music.qq.com/fcgi-bin/dmrp_activity/fcg_dmrp_draw_lottery.fcg?activeid=159&rnd=' . time() . '482&g_tk=' . $this->g_tk . '&uin=' . $this->uin . '&hostUin=0&format=json&inCharset=UTF-8&outCharset=UTF-8&notice=0&platform=activity&needNewCode=1';
        $data = $this->get_curl($url, 0, 'http://y.qq.com/vip/fuliwo/index.html', $this->cookie);
        $arr = json_decode($data, true);
        if (array_key_exists('code', $arr) && $arr['code'] == 0) {
            $this->msg[] = '绿钻抽奖成功！';
        } elseif ($arr['code'] == 200008) {
            $this->msg[] = '您没有抽奖机会！';
        } else {
            $this->msg[] = '绿钻抽奖失败！';
        }

    }

    public function hzqd()
    {
        $url = 'http://vip.qzone.qq.com/fcg-bin/v2/fcg_mobile_vip_site_checkin?t=0.89457' . time() . '&g_tk=' . $this->g_tk . '&qzonetoken='.$this->g_tk2;
        $post = 'uin=' . $this->uin . '&format=json';
        $referer = 'http://h5.qzone.qq.com/vipinfo/index?plg_nld=1&source=qqmail&plg_auth=1&plg_uin=1&_wv=3&plg_dev=1&plg_nld=1&aid=jh&_bid=368&plg_usr=1&plg_vkey=1&pt_qzone_sig=1';
        $data = $this->get_curl($url, $post, $referer, $this->cookie);
        $arr = json_decode($data, true);
        if (array_key_exists('code', $arr) && $arr['code'] == 0) {
            $this->msg[] = '黄钻签到成功！';
        } elseif (array_key_exists('code', $arr) && $arr['code'] == -3000) {
            $this->skeyzt = 1;
            $this->msg[] = '黄钻签到失败！SKEY已失效';
        } elseif (array_key_exists('code', $arr)) {
            $this->msg[] = '黄钻签到失败！' . $arr['message'];
        } else {
            $this->msg[] = '黄钻签到失败！' . $data;
        }

        $url = 'http://activity.qzone.qq.com/fcg-bin/fcg_qzact_count?g_tk=' . $this->g_tk1 . '&format=json&actid=101&uin=' . $this->uin . '&_=' . time() . '3333';
        $data = $this->get_curl($url, 0, $url, $this->cookie);
        $data = mb_convert_encoding($data, "UTF-8", "GB2312");
        $arr = json_decode($data, true);
        $count = @$arr['data']['rule']['1001']['count'][1]['left'];
        while ($count > 0) {
            $url = 'http://activity.qzone.qq.com/fcg-bin/fcg_qzact_lottery?g_tk=' . $this->g_tk1;
            $post = 'actid=101&ruleid=1001&format=json&uin=' . $this->uin . '&g_tk=' . $this->g_tk1 . '&qzreferrer=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fqzact%2Fact%2Fhkhd%2Findex.html';
            $referer = 'http://qzs.qq.com/qzone/qzact/act/hkhd/index.html';
            $data = $this->get_curl($url, $post, $referer, $this->cookie);
            $arr = json_decode($data, true);
            if (array_key_exists('code', $arr) && $arr['code'] == 0) {
                $this->msg[] = '黄钻抽奖成功！';
            } elseif ($arr['code'] == -3000) {
                $this->skeyzt = 1;
                $this->msg[] = '黄钻抽奖失败！SKEY已失效';
            } elseif ($arr['code'] == -5004) {
                $this->msg[] = '黄钻抽奖失败！抽奖机会已用完';
            } elseif (array_key_exists('code', $arr)) {
                $this->msg[] = '黄钻抽奖失败！' . $arr['message'];
            } else {
                $this->msg[] = '黄钻抽奖失败！' . $data;
            }
        }
    }


    private function getGTK($skey)
    {
        $len = strlen($skey);
        $hash = 5381;
        for ($i = 0; $i < $len; $i++) {
            $hash += (($hash << 5) & 0xffffffff) + ord($skey[$i]);
        }
        return $hash & 0x7fffffff;//计算g_tk
    }

    private function getGTK2($skey)
    {
        $salt = 5381;
        $md5key = 'tencentQQVIP123443safde&!%^%1282';
        $hash = array();
        $hash[] = ($salt << 5);
        $len = strlen($skey);
        for ($i = 0; $i < $len; $i++) {
            $ASCIICode = mb_convert_encoding($skey[$i], 'UTF-32BE', 'UTF-8');
            $ASCIICode = hexdec(bin2hex($ASCIICode));
            $hash[] = (($salt << 5) + $ASCIICode);
            $salt = $ASCIICode;
        }
        $md5str = md5(implode($hash) . $md5key);
        return $md5str;
    }

    private function get_curl($url, $post = 0, $referer = 1, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $httpheader[] = "Accept:application/json";
        $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
        $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
        $httpheader[] = "Connection:close";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
        if ($post) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        if ($header) {
            curl_setopt($ch, CURLOPT_HEADER, TRUE);
        }
        if ($cookie) {
            curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        }
        if ($referer) {
            if ($referer == 1) {
                curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
            } else {
                curl_setopt($ch, CURLOPT_REFERER, $referer);
            }
        }
        if ($ua) {
            curl_setopt($ch, CURLOPT_USERAGENT, $ua);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.5 Mobile Safari/533.1');
        }
        if ($nobaody) {
            curl_setopt($ch, CURLOPT_NOBODY, 1);
        }
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        curl_close($ch);
        //$ret=mb_convert_encoding($ret, "UTF-8", "UTF-8");
        return $ret;
    }


}